#include "config.hpp"

#include <string.h>
#include <stdio.h>

PreprocessingOption::PreprocessingOption(){

    type=NEURON;

    //neuron
    erosionradius=4;
    areafilter=150;


    //retinal
    densitysamplex=30;
    densitysampley=45;
    circleradius=37;

    preprocessing_scale=1.0f;

}


Config::Config(){
    invertimage=false;
}

Config::Config(string filename){
    FILE *file;

    file = fopen(filename.c_str(),"r");

    char tag[40];
    char specifier;
    int value;
    float floatvalue;


    if (file == NULL)
     {
        cerr << "File could not be opened\n" << endl;
        exit( 12 );
     }

    cout << "file loaded\n\n";

    while(fscanf(file,"%c",&specifier)==1){
        if(specifier == '#'){
            do{
                fscanf(file,"%c",&specifier);
            }while(specifier!='\n');
            continue;
        }
        if(specifier == '<'){
            fscanf(file,"%s =",tag);
            if(!strncmp(tag,(char*)"critical_distance",17)){
                fscanf(file," %d>\n",&value);
                labellingoptions.critical_distance=value;
                continue;
            }
            else if(!strncmp(tag,"superposition_threshold_direct",30)){
                fscanf(file," %f>\n",&floatvalue);
                labellingoptions.superposition_threshold_direct=floatvalue;
                continue;
            }
            else if(!strncmp(tag,"superposition_threshold_opposite",32)){
                fscanf(file," %f>\n",&floatvalue);
                labellingoptions.superposition_threshold_opposite=floatvalue;
                continue;
            }
            else if(!strncmp(tag,"acute_threshold",15)){
                fscanf(file," %f>\n",&floatvalue);
                labellingoptions.acute_threshold=floatvalue;
                continue;
            }
            else if(!strncmp(tag,"opposite_threshold",18)){
                fscanf(file," %f>\n",&floatvalue);
                labellingoptions.opposite_threshold=floatvalue;
                continue;
            }
            else if(!strncmp(tag,"stabilitycondition",18)){
                fscanf(file," %d>\n",&value);
                labellingoptions.stabilitycondition=value;
                continue;
            }
            else if(!strncmp(tag,"stabilitycondition",18)){
                fscanf(file," %d>\n",&value);
                labellingoptions.stabilitycondition=value;
                continue;
            }
            else if(!strncmp(tag,"preprocessing_scale",18)){
                fscanf(file," %f>\n",&floatvalue);
                preprocessing.preprocessing_scale=floatvalue;
                continue;
            }
            else if(!strncmp(tag,"object",18)){
                char stringlet[20];
                fscanf(file," %s>\n",stringlet);
                if(!strncmp(stringlet,"NEURON",6))
                    preprocessing.type=preprocessing.NEURON;
                if(!strncmp(stringlet,"RETINAL",7))
                     preprocessing.type=preprocessing.RETINAL_VESSELS;
                continue;
            }
            else if(!strncmp(tag,"erosionradius",13)){
                fscanf(file," %d>\n",&value);
                preprocessing.erosionradius=value;
                continue;
            }
            else if(!strncmp(tag,"areafilter",10)){
                fscanf(file," %d>\n",&value);
                preprocessing.areafilter=value;
                continue;
            }
            else if(!strncmp(tag,"densitysamplex",14)){
                fscanf(file," %d>\n",&value);
                preprocessing.densitysamplex=value;
                continue;
            }
            else if(!strncmp(tag,"densitysampley",14)){
                fscanf(file," %d>\n",&value);
                preprocessing.densitysampley=value;
                continue;
            }
            else if(!strncmp(tag,"circleradius",12)){
                fscanf(file," %d>\n",&value);
                preprocessing.circleradius=value;
                continue;
            }
            else if(!strncmp(tag,"invertimage",11)){
                char stringlet[20];
                fscanf(file," %s>\n",stringlet);
                if(!strncmp(stringlet,"TRUE",4))
                    invertimage=true;
                if(!strncmp(stringlet,"FALSE",5))
                    invertimage=false;
                continue;
            }
            else{
                fscanf(file," %d>\n",&value);
                cout << "File error: TAG error\n";
                continue;
            }
        }
        if(specifier=='['){
            fscanf(file,"END]");
            break;
        }
    }
    fclose(file);

}


void Config::save(string filename){
    FILE *file;
    file = fopen(filename.c_str(),"w");

    if (file == NULL){
        cerr << "File could not be opened\n" << endl;
        exit( 12 );
    }

    fprintf(file,"<critical_distance = %d>\n",labellingoptions.critical_distance);
    fprintf(file,"<superposition_threshold_direct = %f>\n",labellingoptions.superposition_threshold_direct);
    fprintf(file,"<superposition_threshold_opposite = %f>\n",labellingoptions.superposition_threshold_opposite);
    fprintf(file,"<acute_threshold = %f>\n",labellingoptions.acute_threshold);
    fprintf(file,"<opposite_threshold = %f>\n",labellingoptions.opposite_threshold);
    fprintf(file,"<stabilitycondition = %d>\n",labellingoptions.stabilitycondition);

    if(preprocessing.type==preprocessing.NEURON)
        fprintf(file,"<object = %s>\n","NEURON");
    else if(preprocessing.type==preprocessing.RETINAL_VESSELS)
        fprintf(file,"<object = %s>\n","RETINAL_VESSELS");


    fprintf(file,"<erosionradius = %d>\n",preprocessing.erosionradius);
    fprintf(file,"<areafilter = %d>\n",preprocessing.areafilter);
    fprintf(file,"<densitysamplex = %d>\n",preprocessing.densitysamplex);
    fprintf(file,"<densitysampley = %d>\n",preprocessing.densitysampley);
    fprintf(file,"<circleradius = %d>\n",preprocessing.circleradius);
    fprintf(file,"<preprocessing_scale = %f>\n",preprocessing.preprocessing_scale);

    if(invertimage==true)
        fprintf(file,"<invertimage = %s>\n","TRUE");
    else
        fprintf(file,"<invertimage = %s>\n","FALSE");

    fprintf(file,"[END]\n");

    fclose(file);
}
